package com.yuncheng.oaApi.service.impl;

import org.apache.commons.collections.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yuncheng.oaApi.constant.OaFlowConstant;
import com.yuncheng.oaApi.entity.OaXzswzrbh;
import com.yuncheng.oaApi.mapper.OaXzswzrbhMapper;
import com.yuncheng.oaApi.service.IOaXzswzrbhService;

import com.yuncheng.oaApi.service.IOaYcSysOrgService;
import com.yuncheng.oaApi.service.IOaYcSysUserService;
import com.yuncheng.oaApi.utils.GetUuIdUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import com.yuncheng.common.base.service.impl.BaseServiceImpl;

import javax.annotation.Resource;
import java.time.Year;
import java.util.List;

/**
 * @Description: OA-行政事务自然编号
 * @Author: lhb
 * @Date: 2024-05-29
 * @Version: V1.0
 */
@Service
public class OaXzswzrbhServiceImpl extends BaseServiceImpl<OaXzswzrbhMapper, OaXzswzrbh> implements IOaXzswzrbhService {


    @Resource
    @Lazy
    private IOaYcSysUserService ycSysUserService;

    @Resource
    @Lazy
    private IOaYcSysOrgService ycSysOrgService;



    /**
     * 生成行政事务自然编号
     */
    @Override
    public String getFzrbh(String id, String userId) {

        OaXzswzrbh oaXzswzrbh = new OaXzswzrbh();
        //前缀
        String userZbmByUserId = ycSysUserService.getUserZbmByUserId(userId);
        //获取部门名称
        String orgName = ycSysOrgService.getById(userZbmByUserId).getOrgName();
        //获取前缀
        String qzh = getqzh(orgName);
        oaXzswzrbh.setFqz(qzh);
        //中间
        Year year = Year.now();
        int currentYear = year.getValue();
        oaXzswzrbh.setFzj(String.valueOf(currentYear));
        //序号
        //当前最大的序号 然后+1
        Integer i = baseMapper.selectMaxxh();
        if (i == null){
            i = 0;
        }else {
            i += 1;
        }
        oaXzswzrbh.setFxh(i);

        //后缀
        Integer fdjhhz = 1;
        Integer i1 = baseMapper.selectMaxdjhhz(qzh);
        if(i1 != null){
            //如果不为空，再去比对最大的前缀是否跟今天初始化之后日期是否相同，如果相同，就加1，如果不相同，就从1开始
            List<OaXzswzrbh> nkRsDjhgls = baseMapper.selectList(new LambdaQueryWrapper<OaXzswzrbh>().eq(OaXzswzrbh::getFhz, i1)
                    .eq(OaXzswzrbh::getFqz,qzh));
            if (CollectionUtils.isNotEmpty(nkRsDjhgls)){

                boolean equals = nkRsDjhgls.get(0).getFqz().equals(qzh);
                if (equals){
                    fdjhhz = i1 + 1;
                }else{
                    fdjhhz = fdjhhz;
                }

            }else {
                fdjhhz = fdjhhz;
            }
        }else{
            fdjhhz = fdjhhz;
        }
        String strNum = String.valueOf(fdjhhz);
        int length = strNum.length();
        if(length == 1){
            oaXzswzrbh.setFzrbh(qzh +currentYear +"-"+"00" + fdjhhz);
        }else if(length == 2){
            oaXzswzrbh.setFzrbh(qzh +currentYear +"-"+"00" + fdjhhz);
        }
        oaXzswzrbh.setId(GetUuIdUtils.ReplaceUuId());
        oaXzswzrbh.setFqz(qzh);
        oaXzswzrbh.setFhz(fdjhhz);
        baseMapper.insert(oaXzswzrbh);
        return oaXzswzrbh.getFzrbh();
    }


    private final String getqzh(String orgName) {
        if (orgName.equals(OaFlowConstant.KSMC_BGS)){
            return OaFlowConstant.KSMC_BGS_JC;
        }else if (orgName.equals(OaFlowConstant.KSMC_ZLGLYKYK)){
            return OaFlowConstant.KSMC_ZLGLYKYK_JC;
        }else if (orgName.equals(OaFlowConstant.KSMC_YPZCSPCYK)){
            return OaFlowConstant.KSMC_YPZCSPCYK_JC;
        }else if (orgName.equals(OaFlowConstant.KSMC_YPSCCYK)){
            return OaFlowConstant.KSMC_YPSCCYK_JC;
        }else if (orgName.equals(OaFlowConstant.KSMC_YPLTCYK)){
            return OaFlowConstant.KSMC_YPLTCYK_JC;
        }else if (orgName.equals(OaFlowConstant.KSMC_YLQXSPCYK_1)){
            return OaFlowConstant.KSMC_YLQXSPCYK_JC_1;
        }else if(orgName.equals(OaFlowConstant.KSMC_YLQXSPCYK_2)){
            return OaFlowConstant.KSMC_YLQXSPCYK_JC_2;
        }else if (orgName.equals(OaFlowConstant.KSMC_SPHZPPSCYK)){
            return OaFlowConstant.KSMC_SPHZPPSCYK_JC;
        }else if (orgName.equals(OaFlowConstant.KSMC_RJK)){
            return OaFlowConstant.KSMC_RJK_JC;
        }
        return orgName;
    }



}
